import java.math.BigInteger;

public class Leet179 {
    public static void main(String[] args) {
        System.out.println(largestNumber(new int[]{6240,7701,3823,9779,3502,7340,9157,9528,8634,9830}));
    }
    static int[] te;
    static BigInteger bigInteger;
    static public String largestNumber(int[] nums) {
        te = nums.clone();
        bigInteger = new BigInteger("0");
        for (int i = 0; i < nums.length; i++) {
            if(nums[i]!=0){
                dfs(nums,"",i,1);
                nums[i] = te[i];
            }
        }
        return bigInteger.toString();
    }
    static void dfs(int[] nums,String temp,int at,int num){
        temp+=nums[at];
        if (!bigInteger.toString().equals("0") && temp.length()<bigInteger.toString().length() && 0 < new BigInteger(bigInteger.toString().substring(0, temp.length())).compareTo(new BigInteger(temp)))
            return;
        if (num == nums.length){
            bigInteger = bigInteger.max(new BigInteger(temp));
            return;
        }
        nums[at] = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i]!=Integer.MAX_VALUE){
                dfs(nums,temp,i,num+1);
                nums[i] = te[i];
            }
        }
    }
}
